package cn.lds.healthexamspringboot.service.impl;

import cn.lds.healthexamspringboot.domain.HealthExamPackage;
import cn.lds.healthexamspringboot.domain.PackageItem;
import cn.lds.healthexamspringboot.dto.ResponseDTO;
import cn.lds.healthexamspringboot.mapper.HealthExamPackageMapper;
import cn.lds.healthexamspringboot.mapper.PackageItemMapper;
import cn.lds.healthexamspringboot.service.HealthExamPackageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

@Service
public class HealthExamPackageServiceImpl implements HealthExamPackageService {

    @Resource
    private HealthExamPackageMapper healthExamPackageMapper;
    @Resource
    private PackageItemMapper packageItemMapper;

    private final static Logger log = LoggerFactory.getLogger(HealthExamPackageService.class);


    @Override
    public ResponseDTO<List<HealthExamPackage>> selectHealthExamPackageAll() {

        List<HealthExamPackage> healthExamPackages = healthExamPackageMapper.selectList(null);



        return ResponseDTO.buildSuccess(healthExamPackages);
    }

    @Override
    public ResponseDTO<HealthExamPackage> selectHealthExamPackageById(Integer id) {

        HealthExamPackage healthExamPackage = healthExamPackageMapper.selectHealthExamPackageById(id);


        return ResponseDTO.buildSuccess(healthExamPackage);
    }

    @Override
    public ResponseDTO addExamPackage(HealthExamPackage examPackage,Integer[] examIds) throws Exception {

        log.info("添加数据前，他的主键{}",examPackage);



        //添加数据到数据库
        healthExamPackageMapper.insert(examPackage);

        log.info("添加数据后，他的主键{}",examPackage);

        if (Objects.isNull(examIds)){
            throw new Exception("没有项目添加");
        }

        Stream.of(examIds).forEach(id->{
            packageItemMapper.insert(new PackageItem(examPackage.getId(),id));
        });







        return null;
    }


}
